const Service = require('../../../core/service/ApiService');

class AdminDao extends Service {

    async findAdminByAccount(account) {
        const sql = `select account,pwd,admin.town_id,town.name as town_name
            from admin
            inner join town on admin.town_id = town.town_id
            where account = '${account}'
            and status = ${this.constant.ADMIN_STATUS.ENABLE}`;
        const result = await this.model.query(sql, {type: this.model.QueryTypes.SELECT});
        return result.length === 0 ? null : result[0];

    }

    //通过session判断密码是否正确
    async findAdminPwd(account) {
        const info = await this.model.Admin.findOne({
            where: {
                account: account,
                status: this.constant.ADMIN_STATUS.ENABLE
            },
            attributes: ['pwd']
        });
        return info !== null ? info.pwd : null;
    }

    //管理员修改密码
    async updatePwd(account, pwd) {
        await this.model.Admin.update({
            pwd: pwd
        }, {
            where: {
                account: account
            }
        })
    }
}

module.exports = AdminDao;

